/* Autostop
 *
 * Copyright (c) Maciej Gajewski, Grzegorz Latarowski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */

#ifndef SERVER_H
#define SERVER_H

// Qt
#include <QTcpServer>

// local
#include "serverthread.h"

// ============================== server class ====================
class Server : protected QTcpServer
{
public:

	Server() : QTcpServer() {}
	virtual ~Server() {}

	// ---------------- listen ----------------------
	/// Listen for incoming connection infinitely (or unitl error)
	void listen( quint16 port )
	{
		QTcpServer::listen( QHostAddress::Any, port );

		while ( waitForNewConnection(-1) )
		{
			// nothing, processing in incomingConnection()
		}
	}

	// ------------------- incoming connection -------
	virtual void incomingConnection( int socket )
	{
		ServerThread *thread = new ServerThread( socket, this );
		connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
		thread->start();
	}
};

#endif // SERVER_H

// eof 


